#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: logger.py 
@version:
@time: 2021/12/29 
"""
import logging
import logging.handlers
import colorlog
from TapClientCore.T01_init_conf.path_conf import PathConf

log_colors_config = {
    'DEBUG': 'white',
    'INFO': 'cyan',
    'WARNING': 'yellow',
    'ERROR': 'red',
    'CRITICAL': 'bold_red',
}

test_many_agent = True


class Logger:
    def __init__(self, path=PathConf.LOG_PATH):
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)
        rht = logging.handlers.TimedRotatingFileHandler(path, 'H')
        fmt = colorlog.ColoredFormatter(
            fmt='%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s '
                '-> '
                '%(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
            datefmt='%Y-%m-%d  %H:%M:%S',
            log_colors=log_colors_config
        )
        rht.setFormatter(fmt)
        rht.setLevel(logging.DEBUG)
        if not test_many_agent:
            logger.addHandler(rht)
        consoleHandler = logging.StreamHandler()
        consoleHandler.setFormatter(fmt)
        logger.addHandler(consoleHandler)
        self.logger = logger

    def __getattr__(self, key):
        return getattr(self.logger, key)


logger = Logger()
